package com.chapter1.basic.queue;

import java.util.Iterator;

/** 
 * @ClassName: Queue 
 * @Description: 自定义队列
 * @author minjun minjun@bw30.com
 * @date 2015-2-25 下午10:47:31 
 *  
 */
public class Queue<Item> implements Iterable<Item>{
	
	private Node first;
	
	private int size;
	
	private class Node{
		Item item;
		Node next;
	}

	/** 
	* @Title: offer 
	* @Description: 将元素添加到队列
	* @param @param item    设定文件 
	* @return void    返回类型 
	* @throws 
	*/ 
	public void offer(Item item) {
		if(first==null){
			first=new Node();
			first.item=item;
		}else{
			Node f=first;
			while(f.next!=null){
				f=f.next;
			}
			f.next=new Node();
			f.next.item=item;
		}
		size++;
	}

	/** 
	* @Title: poll 
	* @Description: 轮询队列
	* @param @return    设定文件 
	* @return Item    返回类型 
	* @throws 
	*/ 
	public Item poll() {
		if(first==null) return null;
		Node f=first;
		first=f.next;
		size--;
		return f.item;
	}
	
	/**
	 * 
	* @Title: size 
	* @Description: 队列元素数量 
	* @param @return    设定文件 
	* @return int    返回类型 
	* @throws
	 */
	public int size(){
		return size;
	}

	public boolean isEmpty(){
		return size==0;
	}

	@Override
	public Iterator<Item> iterator() {
		return new MyIterator();
	}
	
	private class MyIterator implements Iterator<Item>{
		
		private Node f=first;

		@Override
		public boolean hasNext() {
			return f!=null;
		}

		@Override
		public Item next() {
			Node m=f;
			f=m.next;
			return m.item;
		}

		@Override
		public void remove() {
			
		}
		
	}
}
